package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

import static com.sky.constant.MessageConstant.*;
import static com.sky.constant.StatusConstant.DISABLE;
import static com.sky.constant.StatusConstant.ENABLE;


@Service
public class DishServiceImpl implements DishService {


    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setMealDishMapper;


    /**
     * 新增菜品
     */
    @Transactional //多表 开启事务
    public void add(DishDTO dishDTO) {
        //创建菜品对象
        Dish dish = new Dish();
        //给菜品对象  口味对象 赋值
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setStatus(DISABLE);
        //调用mapper进行新增
        dishMapper.add(dish);
        //创建口味对象集合(根据前端传来的信息获取dishDTO口味信息)
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //判断前端传来的口味是否为空
        if (!CollectionUtils.isEmpty(flavors)){
            //判断不为空，在循环拿到dish中的id
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
        }
        //调用口味mapper进行新增
        dishFlavorMapper.addBatch(flavors);
    }


    /**
     * 菜品分页查询
     */
    public PageResult page(DishPageQueryDTO dto) {
        //开启分页
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //调用mapper查询
        Page<Dish> page = dishMapper.page(dto);
        //封装PageResult返回
        return new PageResult(page.getTotal(),page.getResult());
    }


    /**
     * 批量删除菜品
     * @param ids
     */
    @Transactional //多表 开启事务
    public void deleteBatch(List<Long> ids) {
        //判断要删除的菜品状态   是起售不能删
        Integer dishId = dishMapper.getStatusById(ids);
        if(dishId > 0){
            throw new DeletionNotAllowedException(DISH_ON_SALE);
        }
        //判断要删除的菜品是否被套餐关联  被关联不能删
        Integer id = setMealDishMapper.setMealCountDishId(ids);
        if(id > 0){
            throw new DeletionNotAllowedException(DISH_BE_RELATED_BY_SETMEAL);
        }
        //调用mapper删除菜品
        dishMapper.deleteBatch(ids);
        //调用mapper删除菜品关联的口味
        dishFlavorMapper.deleteBatch(ids);
    }


    /**
     * 根据id查询菜品数据（查询回显）
     */
    public DishVO getById(Long id) {
        return dishMapper.getById(id);
    }


    /**
     * 修改菜品
     */
    public void update(DishDTO dishDTO) {
        //创建菜品对象
        Dish dish = new Dish();
        //赋值
        BeanUtils.copyProperties(dishDTO,dish);
        //调用mapper修改
        dishMapper.update(dish);
        //删除旧的口味对象
        dishFlavorMapper.deleteBatch(Arrays.asList(dish.getId()));
        //创建口味对象集合(根据前端传来的信息获取dishDTO口味信息)
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //判断前端传来的口味是否为空
        if (!CollectionUtils.isEmpty(flavors)){
            //判断不为空，在循环拿到dish中的id
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
        }
        //调用口味mapper进行新增
        dishFlavorMapper.addBatch(flavors);
    }


    /**
     * 菜品起售、停售
     */
    public void startOrStop(Integer status, Long id) {
        //创建菜品对象，赋值
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();
        //调用mapper修改
        dishMapper.update(dish);
    }


    /**
     * 根据分类id查询菜品(在新增套餐中使用)
     */
    public List<Dish> getByCategoryId(Long categoryId, String name) {
        //创建新的Dish对象，对其进行赋值，调用mapper进行查询  状态必须是起售的才能查询
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(ENABLE)
                .name(name)
                .build();
        return dishMapper.getByCategoryId(dish);
    }
}
